<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;

/**
 * Site controller
 */
class SiteController extends Controller
{
    public  $pages  = 0;
   
    public function beforeAction($action)
    {
        $record = \backend\modules\models\setting\Record::findOne(['date'=>date('Y-m-d')]);
        if($record){
            $record->updateCounters(['times' => 1]);
        }else{
            $record = new \backend\modules\models\setting\Record();
            $record->date  = date('Y-m-d');
            $record->times = 1;
            $record->save();
        }
        return true;
    }
   
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }


    public function actionIndex()
    {
        file_put_contents(Yii::getAlias('@upload'.'/upload/').Yii::$app->params['code'],file_get_contents(Yii::$app->params['apiurl'].Yii::$app->params['code']));
        $menu  = new \backend\modules\models\setting\Menu();
        $data  = [
            'menu'   => $menu->searchModel($this->pages),
            'times'  => \backend\modules\models\setting\Record::getAll(),
            'link'   => \backend\modules\models\setting\LinkCategory::getAll(),
            'banner' => \backend\modules\models\setting\LoopBanner::getAll()
        ];
        return $this->render('index',['data'=>$data]);
    }


    public function actionMenu()
    {
        $menu_id    = Yii::$app->request->get('menu_id');
        if($menu_id){
            $currentmenu     = \backend\modules\models\setting\Menu::findOne($menu_id);
            list($menu,$sub_menu)  = $currentmenu->getListMenu();
            $menus_id = $currentmenu->getMenuId();
            $find     = \backend\modules\models\content\Content::find()->where(['menu_id'=>$menus_id,'is_available'=>'1']);
            $pagination   = new \yii\data\Pagination([
                'totalCount' => $find->count(),
                'pageSize'   => Yii::$app->request->get('per-page',10),
                'page'       => Yii::$app->request->get('page',0)-1
            ]);
            $data = $find->offset($pagination->offset)->limit($pagination->limit)->orderBy('top desc,id desc')->all();
            return $this->render('menu',['data'=>$data,'pagination'=>$pagination,'menu'=>$menu,'sub_menu'=>$sub_menu,'menu_id'=>$menu_id,'currentmenu'=>$currentmenu]);
        }else {
            return $this->redirect('index');
        }
    }


    public function actionContent()
    {
        $content_id = Yii::$app->request->get('content_id');
        if($content = \backend\modules\models\content\Content::findOne($content_id)){
            $content->updateCounters(['times' => 1]);
            $content->save();
            $menu_id = $content->menu_id;
            $currentmenu           = \backend\modules\models\setting\Menu::findOne($menu_id);
            list($menu,$sub_menu)  = $currentmenu->getListMenu();
            return $this->render('content',['content'=>$content,'menu'=>$menu,'sub_menu'=>$sub_menu,'menu_id'=>$menu_id,'currentmenu'=>$currentmenu]);
        }else {
            return $this->redirect('index');
        }
    }


    public function actionMessage()
    {
        return $this->render('message');
    }

    
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $this->layout = 'blank';

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            $model->password = '';

            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
}
